﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookShopSample
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
            using (SqlConnection con = new SqlConnection())
            {
                con.ConnectionString = @"Server=PD-01\SQLEXPRESS;Database=book_shop;Trusted_Connection=True;";
                con.Open();
                string strSQL = "SELECT * FROM category";
                SqlCommand myCommand = new SqlCommand(strSQL, con);

                using (SqlDataReader strReader = myCommand.ExecuteReader())
                {
                    while (strReader.Read())
                    {
                        string s = strReader["name"].ToString();
                           lbCategory.Items.Add(s);

                    }
                }
            }
        }

        private void btRemove_Click(object sender, EventArgs e)
        {
            string category = lbCategory.SelectedItem.ToString();
            using (SqlConnection con = new SqlConnection())
            {
                con.ConnectionString = @"Server=PD-01\SQLEXPRESS;Database=book_shop;Trusted_Connection=True;";
                con.Open();
                string strSQL = string.Format("DELETE FROM category WHERE name='{0}'", category);
                SqlCommand myCommand = new SqlCommand(strSQL, con);
                myCommand.ExecuteNonQuery();
                con.Close();
                int del = lbCategory.SelectedIndex;
                lbCategory.Items.RemoveAt(del);
            }
        }
    }
}
